import ajax from '@/utils/ajax'
import {downLoadZip} from "@/utils/zipdownload";

/**
 * 保存记录
 * @param form 表单数据
 * @returns {*}
 */
export const getImportList = function (tableName) {
    return ajax.post('/gen/table/getImportList', {tableName: tableName}, true)
}
/**
 * 导入表格
 * @param form 表单数据
 * @returns {*}
 */
export const importTable = function (tableNameList) {
    return ajax.post('/gen/table/importTable', tableNameList)
}


/**
 * 加载记录详情
 * @param id 记录ID
 * @returns {*}
 */
export const load = function (id) {
    return ajax.post('/gen/table/loadMore', {
        id: id
    })
}

/**
 * 保存记录
 * @param form 表单数据
 * @returns {*}
 */
export const save = function (form) {
    return ajax.post('/gen/table/save', {
        jsonStr: JSON.stringify(form)
    })
}


/**
 * 删除记录
 * @param id 记录ID
 * @returns {*}
 */
export const del = function (tableId) {
    return ajax.post('/gen/table/del', {
        tableId: tableId
    },true)
}
/**
 * 预览代码
 * @param id 记录ID
 * @returns {*}
 */
export const preview = function (tableId) {
    return ajax.post('/gen/table/preview', {
        tableId: tableId
    }, true)
}
/**
 * 生成代码
 * @param id 记录ID
 * @returns {*}
 */
export const gen = function (tableId) {
    downLoadZip('/gen/table/gen/' + tableId, "ruoyi");
}

/**
 * 加载列表
 * @param tblOpts 列表信息
 * @param conditions 条件
 * @returns {*}
 */
export const pagination = function (tblOpts, conditions) {
    let params = {
        pageNo: tblOpts.pageNo || 1,
        pageSize: tblOpts.pageSize || 50,
        conditions: conditions || []
    }
    if (tblOpts.prop) {
        Object.assign(params, {
            sort: tblOpts.prop,
            order: tblOpts.order === 'descending' ? 'desc' : 'asc'
        })
    }
    return ajax.post('/gen/table/pagination', params, false)
}
